//
//  AggrementViewController.m
//  OutesIOS
//
//  Created by norman on 2019/2/24.
//  Copyright © 2019 norman. All rights reserved.
//

#import "AggrementViewController.h"
#import "UITapView.h"
#import "UITapImageView.h"
#import "UITapLabel.h"

@interface AggrementViewController ()

@property (nonatomic,strong) UIWebView *webView;

@property (nonatomic,strong) UITapImageView *aggree;

@property (nonatomic,strong) UIButton *loginBtn;

@end

@implementation AggrementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
    //[self loadWebData];
    // Do any additional setup after loading the view.
}


#pragma mark - 初始化视图
- (void)setupViews{
    self.view.backgroundColor = CM_TABLE_BG_COLOR;
//    self.title = @"用户须知";
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreen_Height - kTab_Bar_Height, kScreen_Width, kTab_Bar_Height)];
    if (iPhone_X) {
        bgView.y -= KIphoneX_Adjust_Height;
    }
    [self.view addSubview:bgView];
    
    UIImage *img = [UIImage imageNamed:@"ic_unchecked"];
    self.aggree = [[UITapImageView alloc]initWithImage:img];
    self.aggree.x = 15;
    self.aggree.centerY = bgView.height/2;
    [bgView addSubview:self.aggree];
    self.aggree.tag = 0;
    @weakObj(self)
    [self.aggree addTargetBlock:^(id obj) {
        if (Weakself.aggree.tag == 0) {
            Weakself.aggree.image = [UIImage imageNamed:@"ic_checked"];
            Weakself.aggree.tag = 1;
            Weakself.loginBtn.enabled = YES;
        } else {
            Weakself.aggree.image = [UIImage imageNamed:@"ic_unchecked"];
            Weakself.aggree.tag = 0;
            Weakself.loginBtn.enabled = NO;
        }
    }];
    
    NSString *tipStr = @"我已阅读并同意该协议";
    CGSize s = [Tools sizeOfSingleLineText:tipStr withSize:14];
    UITapLabel *tip = [[UITapLabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.aggree.frame) + 5, 0, s.width, s.height)];
    tip.text = tipStr;
    tip.centerY = bgView.height/2;
    tip.textColor = CM_TABLE_TITLE_COLOR;
    tip.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:tip];
    [tip addTargetsBlock:^(id obj) {
        Weakself.aggree.image = [UIImage imageNamed:@"ic_checked"];
        Weakself.aggree.tag = 1;
        self.loginBtn.enabled = YES;
    }];
    
    UIButton *loginBtn = [UIButton buttonWithStyle:StrapSuccessStyle andTitle:@"去登录" andFrame:CGRectMake(kScreen_Width*2/3 + 10, 0, kScreen_Width/3 - 20, 30) target:self action:@selector(goLogin)];
    [loginBtn setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
    //    [loginBtn setBackgroundColor:CM_ORANGE_COLOR];
    [loginBtn setBackgroundImage:[UIImage imageWithColor:CM_ORANGE_COLOR] forState:UIControlStateNormal];
    [loginBtn setBackgroundImage:[UIImage imageWithColor:UIColor16Alpha(0xEE6611, 0.5)] forState:UIControlStateDisabled];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    loginBtn.centerY = bgView.height/2;
    [bgView addSubview:loginBtn];
    loginBtn.enabled = false;
    self.loginBtn = loginBtn;
    
    self.webView = [[UIWebView alloc] initWithFrame:kScreen_Bounds];
    self.webView.height -= kTab_Bar_Height;
    if (iPhone_X) {
        self.webView.height -= KIphoneX_Adjust_Height;
    }
    self.webView.delegate = self;
    
    self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    self.webView.scalesPageToFit = YES;
    self.webView.multipleTouchEnabled = YES;
    self.webView.userInteractionEnabled = YES;
    
//    self.webView.scalesPageToFit = YES;
    //self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, self.view.height - kNavigation_Bar_Height - kStatus_Bar_Height - KAction_Bar_Height)];
    //    NSString *url = [NSString stringWithFormat:HTTP_GET_PROJECT_HTML_REPORT, kProject.project_id];
    //    NSString *url = @"http://www.baidu.com";
    NSURLRequest *request =[NSURLRequest requestWithURL:[Tools getResourceFullPathURL:@"Doc/aggrement.pdf"]];
    [self.view addSubview: self.webView];
    [MBProgressHUD showMessage:@"数据加载中..." toView:self.view];
    [self.webView loadRequest:request];
}

- (void)goLogin {
    [Tools saveApplicationData:[NSNumber numberWithInt:1] forKey:@"aggrement"];
    [self showLoginViewController];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [MBProgressHUD hideHUDForView:self.view];
    
//    NSString *javascript = [NSString stringWithFormat:@"var viewPortTag=document.createElement('meta');  \
//                            viewPortTag.id='viewport';  \
//                            viewPortTag.name = 'viewport';  \
//                            viewPortTag.content = 'width=%d; initial-scale=%f; minimum-scale=0.2; maximum-scale=2.0; user-scalable=yes;';  \
//                            document.getElementsByTagName('head')[0].appendChild(viewPortTag);" , (int)webView.bounds.size.width, self.rate];
    
//    [webView stringByEvaluatingJavaScriptFromString:javascript];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
